package com.dtw.ditouwang.ui.more;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.databinding.ItemEditorChoosePhotoBinding;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.home.ImagePeopleAdapter;

import java.util.List;

public class EditorChoosePhotoAdapter extends RecyclerView.Adapter<EditorChoosePhotoAdapter.Holder> {
    private List<MoreBean.DataBean> dataBeanList;
    private OnItemClickListener onItemClickListener;

    public EditorChoosePhotoAdapter(List<MoreBean.DataBean> dataBeanList) {

        this.dataBeanList = dataBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemEditorChoosePhotoBinding binding = DataBindingUtil.inflate(LayoutInflater
                .from(viewGroup.getContext()), R.layout.item_editor_choose_photo, viewGroup, false);
        return new Holder(binding);
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int i) {
        holder.bindto(dataBeanList.get(i));
        if("2".equals(dataBeanList.get(i).getType())) {
            holder.imageView.setVisibility(View.VISIBLE);
            holder.recyclerView.setVisibility(View.GONE);
            holder.viewDataBinding.imgHeadFrist.setVisibility(View.GONE);
            holder.viewDataBinding.textName.setVisibility(View.GONE);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(view.getId(),holder.getAdapterPosition(),0);
                }
            });
        }else{
            holder.imageView.setVisibility(View.GONE);
            holder.recyclerView.setVisibility(View.VISIBLE);
            holder.viewDataBinding.imgHeadFrist.setVisibility(View.VISIBLE);
            holder.viewDataBinding.textName.setVisibility(View.VISIBLE);
            holder.recyclerView.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 3));
            ImagePeopleAdapter imagePeopleAdapter = new ImagePeopleAdapter(dataBeanList.get(i).getImg());
            holder.recyclerView.setAdapter(imagePeopleAdapter);
            imagePeopleAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int viewID, int position, int state) {
                    onItemClickListener.onItemClick(viewID, holder.getAdapterPosition(), 0);
                }
            });
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        ItemEditorChoosePhotoBinding viewDataBinding;
        RecyclerView recyclerView;
        ImageView imageView;

        public Holder(ItemEditorChoosePhotoBinding binding) {
            super(binding.getRoot());
            this.viewDataBinding=binding;
            recyclerView = itemView.findViewById(R.id.recyclerview);
            imageView=binding.imageImage;
            binding.imgHeadFrist.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(view.getId(),getAdapterPosition(),0);
                    }
                }
            });
        }

        void bindto(MoreBean.DataBean dataBean){
            viewDataBinding.setPhoto(dataBean);
            viewDataBinding.executePendingBindings();
        }
    }
}
